<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseAdminController;
use App\Lib\Enum\ValRule;
use App\Lib\Logger;
use App\Lib\Validates;
use App\Models\Admin\Org;
use App\Rules\CheckRule;
use App\Service\OrgService;
use Illuminate\Http\Request;

class OrgController extends BaseAdminController {
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) {
        $params = $request->all();
        $data = (new OrgService("Admin.Org"))->getAll(["id","parent_id","name","children_num","sort","description"],$params);
        $result = !empty($data)?$this->success($data):$this->error(70000,"没有组织机构");
        return view("admin.org.index",["error"=>$result["error"],"orgs"=>json_encode($result["data"])]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() {
        $params = (new CheckRule($this->request->all(), ValRule::PARENTID, ValRule::PARENTID_MSG))->check();
        if ($params["error"] > 0) {
            return $this->error($params['error'], $params['msg']);
        }
        //获取父栏目名称
        $result = (new OrgService())->getParent($params["data"]["parent_id"]);
        //结果处理
        return view("admin.org.create",["parent" => $result]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store() {
        $params = (new CheckRule($this->request->all()["data"],ValRule::ORG,ValRule::ORG_MSG))->check();
        if($params['error']>0){
            return $this->error($params['error'], $params['msg']);
        }
        //保存
        $result = (new OrgService())->add($params["data"]);
        if($result['error']>0){
            $this->log(Logger::LOG_LEVEL_ERROR,Logger::ORG,'store',$result['msg'],$result);
            return $this->error($result['error'], $result['msg']);
        }
        $this->log(Logger::LOG_LEVEL_INFO,Logger::ORG,'store',$result['msg'],$result);
        return $this->success("",$result['msg']);
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show(Org $org) {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Org $org) {
        //获取select组织机构
        $orgs=(new OrgService())->orgList([]);
        return view("admin.org.edit",["org"=>$org,"orgs"=>json_encode($orgs)]);
    }

    /**
     * 编辑下拉选择框
     */
    public function select(){
        //获取所有机构
        return (new OrgService())->orgList([]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update($id) {
        //验证
        $check = Validates::isPositiveInteger($id);
        if(!$check){
            return $this->tempNotice('admin.org.index',"组织机构不存在","error");
        }
        $params = (new CheckRule($this->request->all()["data"],ValRule::ORG,ValRule::ORG_MSG))->check();
        if($params['error']>0){
            return $this->error($params['error'], $params['msg']);
        }
        //更新
        $result = (new OrgService())->updateInfo($this->request->all()["data"],$id);
        if($result["error"]>0){
            $this->log(Logger::LOG_LEVEL_ERROR,Logger::ORG,'update',$result['msg'],$result);
            return $this->error($result["error"],$result["msg"]);
        }
        $this->log(Logger::LOG_LEVEL_INFO,Logger::ORG,'update','更新成功',$result);
        // todo 更新课程的组织机构code
        return $this->success("","更新成功");
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        //验证
        $check = Validates::isPositiveInteger($id);
        if(!$check){
            return $this->tempNotice("admin.org.index","id必须是正整数","error");
        }
        //todo 处理该机构下的课程人员到父级
        //删除
        $result = (new OrgService())->delete($id);
        if($result["error"]>0){
            $this->log(Logger::LOG_LEVEL_ERROR,Logger::ORG,'destroy',$result['msg'],['del_id'=>$id]);
            return $this->layuiDataError($result['error'],$result['msg']);
        }
        $this->log(Logger::LOG_LEVEL_INFO,Logger::ORG,'destroy',$result['msg'],['del_id'=>$id]);
        return $this->layuiDataSuccess("",$result['error'],$result['msg']);
    }

    /**
     * 排序
     */
    public function setSort($id,$sort){
        //验证
        $params = (new CheckRule(["id"=>$id,"sort"=>$sort], ValRule::SORT, ValRule::SORT_MSG))->check();
        if ($params["error"] > 0) {
            return $this->error($params['error'], $params['msg']);
        }
        //排序
        $result = (new OrgService())->setSort($id,$sort);
        if ($result) {
            $this->log(Logger::LOG_LEVEL_INFO, Logger::CATEGORY, 'setSort', "分类[ID:{$id}]更新排序,成功", "");
            return $this->success("", "排序更新成功");
        }
        $this->log(Logger::LOG_LEVEL_ERROR, Logger::CATEGORY, 'setSort', "分类[ID:{$id}]更新排序,失败", "");
        return $this->error(80000, "排序更新失败");
    }
}
